package cn.dong.edu.service.impl;

import cn.dong.base.dto.CollectDto;
import cn.dong.base.dto.CourseDto;
import cn.dong.edu.domain.CourseCollect;
import cn.dong.edu.mapper.CourseCollectMapper;
import cn.dong.edu.service.CourseCollectService;
import cn.dong.edu.service.CourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-24
 */
@Service
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollect> implements CourseCollectService {

    @Autowired
    private CourseService courseService;

    /**
     * 查询这个用户下的所有收藏
     *
     * @param id
     * @return
     */
    @Override
    public List<CollectDto> findAllByMemberId(String id) {
        List<CollectDto> result = new ArrayList<>(10);

        QueryWrapper<CourseCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", id);
        List<CourseCollect> list = super.baseMapper.selectList(wrapper);
        // 循环遍历
        for (CourseCollect courseCollect : list) {
            // 创建结果Dto对象
            CollectDto collect = new CollectDto();
            // 查询信息封装Dto对象
            CourseDto courseDto = courseService.getCourseDto(courseCollect.getCourseId());
            // 填充属性
            collect.setCourseId(courseCollect.getCourseId());
            collect.setCourseTitle(courseDto.getTitle());
            collect.setTeacherName(courseDto.getTeacherName());
            collect.setTotalFee(courseDto.getPrice());
            collect.setId(id);
            collect.setGmtCreate(courseCollect.getGmtCreate());
            result.add(collect);
        }
        return result;
    }
}
